package Sort;

public class MaoP {
    public static void main(String[] args) {
        int[] arr = new int[]{8, 782, 5, 2, 7, 1, 8,6, 8,3 };
        int[] a = b(arr);
        for (int i :
                a) {
            System.out.println(i);
        }
    }

    public static void swap(int[] arr, int i, int j) {
        int t = arr[i];//基本数据类型可以这样 ，但是引用类型就要clone
        arr[i] = arr[j];
        arr[j] = t;

    }

    public static int[] a(int[] arr) {
        int flag = 0;
        while (true) {
            int f = 1;
            int b = 0;
            while (f <= arr.length - 1) {
                if (arr[f] < arr[b]) {
                    swap(arr, b, f);
                    flag++;
                }
                b++;
                f++;
            }
            if (flag == 0) {
                return arr;
            }
            flag = 0;

        }


    }

    public static int[] b(int[] arr) {
        int last=arr.length-1;
        int flag = 0;
        while (true) {
            int f = 1;
            int b = 0;
            for (int i = 0; i < last; i++) {
                if (arr[b] > arr[f]) {
                    swap(arr, b, f);
                    flag++;
                    System.out.println("第"+i+"次比较");
                }
                b++;
                f++;
            }
            if (flag==0){
                return arr;
            }
             flag=0;
            last=b;
            System.out.println("冒泡");
        }


    }

}
